package com.wfl.sampling.web;
import com.wechat.pay.contrib.apache.httpclient.auth.Verifier;
import com.wfl.sampling.aop.NoAuth;
import com.wfl.sampling.entity.User;
import com.wfl.sampling.entity.WxchatOrderInformation;
import com.wfl.sampling.entity.WxchatRefundInformation;
import com.wfl.sampling.handler.*;
import com.wfl.sampling.service.WxPayService;
import com.wfl.sampling.service.WxchatSignService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.MalformedURLException;
import java.net.URISyntaxException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.security.SignatureException;
import java.security.spec.InvalidKeySpecException;
import java.util.List;
import java.util.Map;

//!!!!用于发起和接收支付接口!!!!!!
@CrossOrigin
@Slf4j
@RestController
@RequestMapping("/api/WxPay")
public class WxchatPayConnector {
    @Autowired
    private WxPayService wxPayService;
    @Autowired
    WxchatSignService wxchatSignService;

    //支付下单接口(返回 code activity_id)
    @PostMapping("/GetOrder")
    public WxChatPayResponseInfo appPay(@RequestBody @Validated PayInRequestinfo payInRequestinfo, HttpServletRequest request) throws IOException, NoSuchAlgorithmException, SignatureException, InvalidKeySpecException, InvalidKeyException {
        // 调用服务层方法发起预下单请求
        return wxPayService.unifiedOrder(payInRequestinfo,request);
    }

    //微信支付通知回调接口
    @PostMapping ("/callbackOrder")
    public String Order_Notify_Url(HttpServletRequest request, HttpServletResponse response) throws IllegalAccessException, IOException {
        return wxPayService.verifySignatureOrder(request,response,wxchatSignService.getVerifier());
    }

    //退款申请接口
    /**
     * 查询微信小程序支付订单
     * @param body  out_refund_no,amount 必要参数
     * @return 订单详情
     */
    @PostMapping("/api/GetRefund")
    public String requestRefund(@RequestBody String body) throws IOException, NoSuchAlgorithmException, SignatureException, InvalidKeySpecException, InvalidKeyException {
        // 调用服务层方法发起退款请求
        return wxPayService.refund(body);
    }

    //微信退款通知回调接口
    @PostMapping("/callbackRefund")
    public String Refund_Notify_Url(HttpServletRequest request, HttpServletResponse response) throws IllegalAccessException, IOException {
        return wxPayService.verifySignatureRefund(request,response,wxchatSignService.getVerifier());
    }

    //支付结果查询接口
    /**
     * 查询微信小程序支付订单
     *
     * @param order_id 订单ID
     * @return 订单详情
     */
    @GetMapping("/queryWeChatPayOrder/{order_id}")
    public WxchatOrderInformation queryWeChatPayOrder(@PathVariable String order_id) throws IOException, URISyntaxException, NoSuchAlgorithmException, SignatureException, InvalidKeySpecException, InvalidKeyException {
        // 调用服务层方法查询订单详情
        return wxPayService.queryOrder(order_id);
    }


    //退款结果查询接口
    @GetMapping("/queryWxChatRefund/{PrePay_id}")
    public WxchatRefundInformation queryWxChatRefund(@PathVariable String PrePay_id) throws MalformedURLException, UnsupportedEncodingException, URISyntaxException, NoSuchAlgorithmException, SignatureException, InvalidKeySpecException, InvalidKeyException {
        return wxPayService.queryRefund(PrePay_id);
    }

    //关闭订单接口
    @GetMapping("/CloseOrder/{order_id}")
    public Map<String, Integer> CloseOrder(@PathVariable String order_id) throws UnsupportedEncodingException, NoSuchAlgorithmException, SignatureException, InvalidKeySpecException, InvalidKeyException {
        return wxPayService.Closeorder(order_id);
    }

    //查询订单列表
    @PostMapping("/findOrders/{user_id}")
    public List<WxchatOrderInformation> findOrders(@PathVariable int user_id){
        return wxPayService.findOrders(user_id);
    }

}
